package com.eascs.p2p.wallet.dao.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.p2p.common.dao.BaseDao;
import com.eascs.p2p.common.entity.InvestorWalletEntity;
import com.eascs.p2p.common.entity.InvestorWalletFlowEntity;
import com.eascs.p2p.common.entity.InvestorWalletPayEntity;
import com.eascs.p2p.common.entity.InvestorWithdrawRecordEntity;
import com.eascs.p2p.common.vo.InvestorWithdrawRecordVO;
import com.eascs.p2p.wallet.dao.InvestorWalletDao;

@Repository
public class InvestorWalletDaoImpl extends BaseDao implements InvestorWalletDao {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Override
	public InvestorWalletEntity queryInvestorWallet(String investorId) {
		String sql="select * from p2p_investor_wallet where investor_id=?";
		return this.findEntity(sql, InvestorWalletEntity.class, investorId);
	}

	@Override
	public Integer insertInvestorWallet(InvestorWalletEntity investorWallet) {
		BigDecimal bg=new BigDecimal(0);
		if (investorWallet.getCurrentBalance() == null) {
			investorWallet.setCurrentBalance(bg);
		}
		if (investorWallet.getInvestFrozen() == null) {
			investorWallet.setInvestFrozen(bg);
		}
		if (investorWallet.getWithdrawFrozen() == null) {
			investorWallet.setWithdrawFrozen(bg);
		}
		if (investorWallet.getAccumulateWithdraw() == null) {
			investorWallet.setAccumulateWithdraw(bg);
		}
		if (investorWallet.getAccumulateRecharge() == null) {
			investorWallet.setAccumulateRecharge(bg);
		}
		if (investorWallet.getAccumulateIncome() == null) {
			investorWallet.setAccumulateIncome(bg);
		}
		if (investorWallet.getAwaitPrincipal() == null) {
			investorWallet.setAwaitPrincipal(bg);
		}
		if (investorWallet.getAwaitIncome() == null) {
			investorWallet.setAwaitIncome(bg);
		}
		investorWallet.setCreateTime(new Date());
		investorWallet.setUpdateTime(new Date());
		return this.p2pSession.insert(investorWallet);
	}

	@Override
	public Integer updateInvestorWallet(InvestorWalletEntity investorWallet) {
		investorWallet.setUpdateTime(new Date());
		return this.p2pSession.updateById(investorWallet);
	}

	@Override
	public PageResult<InvestorWalletFlowEntity> findInvestorWalletFlowByPage(String investorId,int transType, int pageSize, int pageIndex) {
		String sql="select * from p2p_investor_wallet_flow where investor_id=?";
		List<Object> paramsList = new ArrayList<>();
		paramsList.add(investorId);
		if(transType != -1 && transType != 0){
			sql += " and trans_type=?";
			paramsList.add(transType);
		}
		Object[] params = paramsList.toArray();
		SqlPageQuery<InvestorWalletFlowEntity> pageQuery = new SqlPageQuery<InvestorWalletFlowEntity>(InvestorWalletFlowEntity.class, pageSize, pageIndex,sql);
		pageQuery.addParams(params);
		return this.findDataWithPage(pageQuery);
	}

	@Override
	public Integer insertInvestorWalletFlow(InvestorWalletFlowEntity investorWalletFlow) {
		investorWalletFlow.setCreateTime(new Date());
		investorWalletFlow.setUpdateTime(new Date());
		return this.p2pSession.insert(investorWalletFlow);
	}

	@Override
	public Integer insertInvestorWalletPay(InvestorWalletPayEntity investorWalletPay) {
		investorWalletPay.setCreateTime(new Date());
		investorWalletPay.setUpdateTime(new Date());
		return this.p2pSession.insert(investorWalletPay);
	}

	@Override
	public Integer updateInvestorWalletPay(InvestorWalletPayEntity investorWalletPay) {
		List<Object> param = new ArrayList<>();
		StringBuffer sql = new StringBuffer();
		sql.append("update p2p_investor_wallet_pay set ");
		if (investorWalletPay.getStatus() != 0) {
			sql.append(" status=?,");
			param.add(investorWalletPay.getStatus());
		}
		if (StringUtils.isNotBlank(investorWalletPay.getWalletId())) {
			sql.append(" wallet_id=?,");
			param.add(investorWalletPay.getWalletId());
		}
		sql.append(" update_time=?");
		param.add(new Date());
		sql.append(" WHERE id=?");
		param.add(investorWalletPay.getId());
		return this.p2pSession.execute(sql.toString(), param.toArray());
	}

	@Override
	public InvestorWalletPayEntity queryInvestorWalletPayById(String id) {
		String sql="select * from p2p_investor_wallet_pay where id=?";
		return this.findEntity(sql, InvestorWalletPayEntity.class, id);
	}

	@Override
	public Integer insertWithdrawRecord(InvestorWithdrawRecordEntity withdrawRecord) {
		withdrawRecord.setCreateTime(new Date());
		withdrawRecord.setUpdateTime(new Date());
		return this.p2pSession.insert(withdrawRecord);
	}

	@Override
	public Integer updateWithdrawRecord(InvestorWithdrawRecordEntity withdrawRecord) {
		List<Object> param = new ArrayList<>();
		StringBuffer sql = new StringBuffer();
		sql.append("update  p2p_investor_withdraw_record set ");
		if (withdrawRecord.getStatus() != 0) {
			sql.append(" status=?,");
			param.add(withdrawRecord.getStatus());
		}
		if(withdrawRecord.getStatus() ==2||withdrawRecord.getStatus() ==3){
			sql.append(" verifier=?,");
			param.add(withdrawRecord.getVerifier());
		}
		
	
		sql.append(" update_time=?");
		param.add(new Date());
		sql.append(" WHERE id=?");
		param.add(withdrawRecord.getId());
		return this.p2pSession.execute(sql.toString(), param.toArray());
	}

	@Override
	public InvestorWithdrawRecordEntity queryWithdrawRecordByrequestno(String requestno) {
		String sql="select * from p2p_investor_withdraw_record where requestno=?";
		return this.findEntity(sql, InvestorWithdrawRecordEntity.class, requestno);
	}

	@Override
	public InvestorWithdrawRecordEntity queryWithdrawRecordById(String id) {
		String sql="select * from p2p_investor_withdraw_record where id=?";
		return this.findEntity(sql, InvestorWithdrawRecordEntity.class, id);
	}

	@Override
	public  PageResult<Object[]> findPageInvestorWithdrawRecord(InvestorWithdrawRecordVO vo) {
	
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getShowPage());
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT record.id,requestno, investor.name,apply_amount,record.create_time,item.item_name from p2p_investor_withdraw_record record");
		sql.append(" LEFT JOIN p2p_investor investor ON record.investor_id= investor.id");
		sql.append(" LEFT JOIN p2p_datadic_item item ON item.item_code = record.status AND item.dd_no = 15 where 1=1");
		// 提现流水号
		if (StringUtil.isNotBlank(vo.getRequestno())) {
			sql.append(" and requestno like ?");
			
			sqlPageQuery.addParam("%" + vo.getRequestno()+ "%");

		}
		
		// 提现人姓名
		if (StringUtil.isNotBlank(vo.getName())) {
			sql.append(" and investor.name like ?");
			sqlPageQuery.addParam("%" + vo.getName() + "%");
		}
		
		//状态
		if (StringUtil.isNotBlank(vo.getStatus())) {
			sql.append(" and  record.status = ?");
			sqlPageQuery.addParam( vo.getStatus() );
		}
		
		// 创建时间
		if (StringUtil.isNotBlank(vo.getBeginCreateTime())) {

			sql.append(" and date_format(record.create_time,'%Y-%m-%d') >= ?");
			sqlPageQuery.addParam(vo.getBeginCreateTime());
		}
		if (StringUtil.isNotBlank(vo.getEndCreateTime())) {

			sql.append(" and date_format(record.create_time,'%Y-%m-%d') <= ?");
			sqlPageQuery.addParam(vo.getEndCreateTime());
		}
		
		sql.append(" order by record.create_time desc ");
		sqlPageQuery.setQuerySql(sql.toString());
		PageResult<Object[]> page = this.findDataWithPage(sqlPageQuery);
		return page;
	}

}
